package cfca.paperless.client.servlet._8_local.suning;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessClientLocal;
import cfca.paperless.client.servlet.PaperlessLocalConfig;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.StringUtil;
import cfca.sadk.x509.certificate.X509Cert;

/**
 * <pre>
 * Modify Information:
 * Author       Date          Description
 * ============ ============= ============================
 * zf          2018年4月9日          查询印章信息、签普通章
 * </pre>
 */
public class QuerySealAndSignatureLocalTest03 {

    public static void main(String[] args) throws Exception {
        try {
            long timeStart = System.currentTimeMillis();// 开始时间
            
            float coefficient = 0.75f;
            
            //无纸化辅助接口的访问URL
            PaperlessClient clientExtBean = new PaperlessClient(PaperlessLocalConfig.extUrl, 10000, 60000);

            //无纸化辅助接口的访问URL
            PaperlessClient clientAssistBean = new PaperlessClient(PaperlessLocalConfig.assistUrl, 10000, 60000);

            PaperlessClientLocal paperlessClientLocal = new PaperlessClientLocal(clientExtBean, clientAssistBean, PaperlessLocalConfig.operatorCode);
           
            paperlessClientLocal.initTimestampServer("http://127.0.0.1:8080/Proxy/Timestamp", "", "");
            
            //待签章pdf
            String filePath = "./TestData/autoConcatPdfList-local.pdf";
            byte[] pdf = FileUtils.readFileToByteArray(new File(filePath));
            
            // 印章信息
            String sealCode = "yz201804847655019";// 印章编码
            String sealPassword = "cfca1234";// 印章密码
            
            //查询印章信息-证书和图片信息
            X509Cert x509Cert = null;
            byte[] imageData = "".getBytes();
            String result = clientAssistBean.getSealInfo(sealCode, PaperlessLocalConfig.operatorCode);
            
            String code = StringUtil.getNodeText(result, "Code");
            if (!code.equals("200")) {
                throw new Exception(result);
            } else {
                String x509CertStr = StringUtil.getNodeText(result, "SealCert");
                String imageStr = StringUtil.getNodeText(result, "Image");
                x509Cert = new X509Cert(Base64.decode(x509CertStr));
                imageData = Base64.decode(imageStr);
            }
             
            SignLocation signLocation = new SignLocation("投资运作", "R", 0, 0);// 签名位置
 
            //签普通章
            pdf = paperlessClientLocal.sealAutoPdfLocal(pdf, sealCode, sealPassword, x509Cert, imageData, coefficient, signLocation);
            
            IoUtil.write("./TestData/output/sealAutoPdfLocal03.pdf", pdf);
            
            
            long timeEnd = System.currentTimeMillis();// 结束时间
            System.out.println("time:" + (timeEnd - timeStart));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
}
